#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using log4net;
using xConnected.Core.Interfaces.Service;
using xConnected.Core.Model;
using xConnected.Core.Model.ViewModel;

#endregion

namespace xConnected.Controllers
{
    public class CompanyListController : ApiController
    {
        protected ICompanyListService _companyListService;
        protected ICompanyProfileService _companyProfileService;
        protected IUserService _userService;

        public CompanyListController(ICompanyListService companyListService, IUserService userService,
                                     ICompanyProfileService companyProfileService)
        {
            _companyListService = companyListService;
            _userService = userService;
            _companyProfileService = companyProfileService;
        }

        public IEnumerable<CompanyList> List()
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var userId = _userService.GetUserByUsername(User.Identity.Name).Id;

                var companyLists =
                    _companyListService.GetAll().Where(p => p.OwnerId == userId && p.IsBlackList == false).OrderBy(
                        p => p.Id);

                return companyLists;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        public HttpResponseMessage PostCompanyList(CompanyList companyList)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var user = _userService.GetUserByUsername(User.Identity.Name);
                var foundList =
                    _companyListService.GetAll().Where(m => m.OwnerId == user.Id && m.Name == companyList.Name);
                if (!foundList.Any())
                {
                    companyList.OwnerId = user.Id;
                    companyList.IsBlackList = false;

                    _companyListService.SaveOrUpdate(companyList);

                    return new HttpResponseMessage(HttpStatusCode.Created);
                }
                return new HttpResponseMessage(HttpStatusCode.Forbidden);
            }

            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        public HttpResponseMessage AddCompany(BookmarkModel bookmarkModel)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var userId = _userService.GetUserByUsername(User.Identity.Name).Id;

                if (!bookmarkModel.IsBlock)
                {
                    var companyList = _companyListService.GetById(bookmarkModel.CompanyList.Id);
                    var company = _companyProfileService.GetById(bookmarkModel.CompanyProfile.Id);
                    if (!companyList.CompanyProfiles.Select(e => e.Id).Contains(company.Id))
                    {
                        companyList.CompanyProfiles.Add(company);
                        _companyListService.SaveOrUpdate(companyList);
                    }
                }
                else
                {
                    var blockList = _companyListService.GetAll().First(e => e.IsBlackList && e.OwnerId == userId);
                    if (!blockList.CompanyProfiles.Select(e => e.Id).Contains(bookmarkModel.CompanyProfile.Id))
                    {
                        blockList.CompanyProfiles.Add(bookmarkModel.CompanyProfile);
                        _companyListService.SaveOrUpdate(blockList);
                    }
                }

                return new HttpResponseMessage(HttpStatusCode.OK);
            }

            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }
    }
}